/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.deschutter.services;

import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.deschutter.Constants;
import org.deschutter.URLReader;
import org.deschutter.dao.impl.PlanetDAOImpl;
import org.deschutter.dao.impl.PlanetScanDAOImpl;
import org.deschutter.hibernate.Planets;
import org.deschutter.hibernate.Scans;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author berten
 */
public class ParserService implements Constants{
private PlanetScanDAOImpl planetScanDAO;
private PlanetDAOImpl planetDAO;

    public PlanetDAOImpl getPlanetDAO() {
        return planetDAO;
    }

    public void setPlanetDAO(PlanetDAOImpl planetDAO) {
        this.planetDAO = planetDAO;
    }

    public void getPlanetScan() {
        planetDAO.findAll();
    }

    public String parse(String message) {
List<String> scanurls = getScanUrls(message);
        return "";

    }

     public ArrayList<String> getScanUrls(String message) {
        Pattern compiledRegex;
        Matcher regexMatcher;
        ArrayList<String> lijst = new ArrayList<String>();


        try {
            compiledRegex = Pattern.compile("scan_id=[a-z0-9]+");
            regexMatcher = compiledRegex.matcher(message);

            while (regexMatcher.find()) {
                lijst.add(PLANETARION_URL + SCAN_URL + regexMatcher.group());
            }

            compiledRegex = Pattern.compile("scan_grp=[a-z0-9]+");
            regexMatcher = compiledRegex.matcher(message);

            while (regexMatcher.find()) {
                lijst.add(PLANETARION_URL + SCAN_URL + regexMatcher.group());
            }
        } catch (PatternSyntaxException ex) {
            System.out.println("You have an error in your regular expression:\n" +
                    ex.getDescription());
        }

        return lijst;
    }

     public void parse(ArrayList<String> scanurls) {
        for (String url : scanurls) {

            if (url.contains("scan_grp")) {
                retreiveScansFromGroup(url);
            } else {
               parseScan(url);
            }
        }
    }

    public void retreiveScansFromGroup(String url) {
        Pattern compiledRegex;
        Matcher regexMatcher;

        try {
            String page = URLReader.get(url);
            compiledRegex = Pattern.compile("scan_id=[a-z0-9]+");
            regexMatcher = compiledRegex.matcher(page);
            while (regexMatcher.find()) {
                parseScan(PLANETARION_URL + SCAN_URL + regexMatcher.group());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (PatternSyntaxException ex) {
            System.out.println("You have an error in your regular expression:\n" +
                    ex.getDescription());
        }
    }

    public void parseScan(String url) {
        Pattern compiledRegex;
        Matcher regexMatcher;
     //   Scan scan = null;
      //  HashMap scanInfoMap;
        try {
            String scanPage = URLReader.get(url);
            // Get Scan ID
            String scanID = getID(url);

            compiledRegex = Pattern.compile("([^>]+) on (\\d+)\\:(\\d+)\\:(\\d+) in tick (\\d+)");
            regexMatcher = compiledRegex.matcher(scanPage);

          while (regexMatcher.find()) {
                if (regexMatcher.group(1).equals("Planet Scan")) {
                    //scan = new Planet(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                    Scans s = new Scans();
                    s.setId(scanID);
                    s.setTick(new BigDecimal(regexMatcher.group(5)));

                  //  List<Criterion> shit = new ArrayList<Criterion>();

                   // shit.add(Restrictions.eq("x",regexMatcher.group(2))) ;
                   // shit.add(Restrictions.eq("x",regexMatcher.group(3))) ;
                   // shit.add(Restrictions.eq("x",regexMatcher.group(4))) ;


                    planetDAO.findByCriteria(new Criterion[]{Restrictions.eq("tick",new BigDecimal(regexMatcher.group(5))),Restrictions.eq("x",regexMatcher.group(2)),Restrictions.eq("y",regexMatcher.group(3)),Restrictions.eq("z",regexMatcher.group(4))});



                }/* else if (regexMatcher.group(1).equals("Development Scan")) {
                    scan = new Development(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                } else if (regexMatcher.group(1).equals("Unit Scan")) {
                    scan = new Unit(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                } else if (regexMatcher.group(1).equals("News Scan")) {
                    scan = new News(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                } else if (regexMatcher.group(1).equals("Incomming Scan")) {
                    scan = new IncommingScan(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                } else if (regexMatcher.group(1).equals("Jumpgate Probe")) {
                    scan = new Jumpgate(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                } else if (regexMatcher.group(1).equals("Advanced Unit Scan")) {
                    scan = new AdvancedUnit(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                }*/
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
       // return scan;
    }

    public static String getID(String url) {
        String[] a = url.split("=");
        return a[1];
    }

    public PlanetScanDAOImpl getPlanetScanDAO() {
        return planetScanDAO;
    }

    public void setPlanetScanDAO(PlanetScanDAOImpl planetScanDAO) {
        this.planetScanDAO = planetScanDAO;
    }
}
